package ch.drshit.view.swing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollBar;

public class MyJFrame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 16520248431924637L;

	private MyJMenuBar jmenubar;
	private MyJComponent jcomponent;
	private JProgressBar jprogressbar;
	private JScrollBar jscrollhorizontal;
	private JScrollBar jscrollvertical;

	public MyJFrame(String title) {
		super(title);
		this.initComp();
		this.initLayout();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setSize(screenSize);
		setExtendedState(MAXIMIZED_BOTH);
		this.setVisible(true);
	}

	private void initLayout() {
		// this
		this.setLayout(new BorderLayout());

		// MenuBar
		this.setJMenuBar(this.jmenubar);

		// Jcomponent (Main Content) with scrollbar
		JPanel jcenterpanel = new JPanel();
		jcenterpanel.setLayout(new BorderLayout());
		jcenterpanel.add(this.jscrollhorizontal, BorderLayout.SOUTH);
		jcenterpanel.add(this.jscrollvertical, BorderLayout.EAST);
		jcenterpanel.add(this.jcomponent, BorderLayout.CENTER);
		this.add(jcenterpanel,BorderLayout.CENTER);
		
		// JProgressBar
		this.jprogressbar.setMinimum(0);
		this.jprogressbar.setMaximum(100);
		this.add(this.jprogressbar, BorderLayout.SOUTH);
	}

	private void initComp() {
		this.jmenubar = new MyJMenuBar();
		this.jcomponent = new MyJComponent();
		this.jprogressbar = new JProgressBar();
		this.jscrollhorizontal = new JScrollBar(JScrollBar.HORIZONTAL,0,100,0,100);
		this.jscrollhorizontal.setName("h");
		this.jscrollvertical = new JScrollBar(JScrollBar.VERTICAL,0,100,0,100);
		this.jscrollvertical.setName("v");
	}
	
	public void calcScrollbars() {
		if (this.getJcomponent().hasBF()) {
			Dimension framesize = this.jcomponent.getSize();
			Dimension picturesize = this.getJcomponent().getRealSize();
			JScrollBar horizontal = this.getJscrollhorizontal();
			JScrollBar vertical = this.getJscrollvertical();			
			// horizontal
			int extentH = picturesize.width - (picturesize.width - framesize.width);
			extentH = extentH > 0 ? extentH : 0;
			int valueH = this.getJcomponent().getXadjust();
			horizontal.setValues(valueH, extentH, 0, picturesize.width);
			// vertical
			int extentV = picturesize.height - (picturesize.height - framesize.height);
			extentV = extentV > 0 ? extentV : 0;
			int valueV = this.getJcomponent().getYadjust();
			vertical.setValues(valueV, extentV, 0, picturesize.height);
			this.getJcomponent().repaint();
		}
	}
	
	public MyJComponent getJcomponent() {
		return jcomponent;
	}

	public JProgressBar getJprogressbar() {
		return jprogressbar;
	}

	public JScrollBar getJscrollhorizontal() {
		return jscrollhorizontal;
	}

	public JScrollBar getJscrollvertical() {
		return jscrollvertical;
	}
	
}
